package com.power.sifa.approve.model.doc;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.plf.common.domain.YesNo;
import com.plf.common.utils.StringUtil;
import com.power.sifa.approve.model.Apl_admreview;
import com.power.sifa.approve.model.Apl_applicant;
import com.power.sifa.approve.model.Apl_main;
import com.power.sifa.approve.model.enumeration.Tp_admpr;
import com.power.sifa.approve.model.enumeration.Tp_applicant;
import com.power.sifa.approve.service.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.util.CollectionUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @ClassName AbstartDoc
 * @Author xiongzw
 * @Date 2023/11/23 11:36
 * @Description 文书的抽象父类
 * @Version: 1.0
 **/

@Data
@AllArgsConstructor
public abstract class AbstractDoc {

    /**
     * applyid-必带数据
     */
    private String applyid;
    /**
     * 案号-必带数据
     */
    private String apl_admre_audit_code;
    /**
     * 模板id
     */
    private Integer docType;

    /**
     * 申请人
     */
    private List<Apl_applicant> applicantList;

    /**
     * 行政复议申请实体
     */
    private Apl_admreview admreObj;

    /**
     * 业务申请主表
     */
    private Apl_main faMain;

    /**
     * 签署时间
     */
    private String today;


    public AbstractDoc() {
        this.setToday(this.transModelToday(Boolean.FALSE));
    }


    public AbstractDoc(String applyid) {
        this.setToday(this.transModelToday(Boolean.FALSE));
        this.applyid = applyid;
    }

    /**
     * 返回主体代词
     */
    public String transPronoun() {
        // 主体代词
        String pronoun = "";
        if (!CollectionUtils.isEmpty(this.applicantList)) {
            if (this.applicantList.size() > 1) {
                pronoun = "你们";
            } else if (Tp_applicant.CITIZEN == this.applicantList.get(0).getType()) {
                pronoun = "你";
            } else {
                pronoun = "你单位";
            }
        }
        return pronoun;
    }

    /**
     * 返回对应的行为
     *
     * @return
     */
    public String transBehavior() {
        // 判断行为
        String behavior = "";
        if (ObjectUtil.isNotNull(admreObj)) {
            if (ObjectUtil.isNotNull(admreObj.getIsomission()) && YesNo.YES == admreObj.getIsomission()) {
                behavior = "不履行法定职责";
            } else {
                if (ObjectUtil.isNotEmpty(admreObj.getPrtype())) {
                    if (Tp_admpr.OTHER.getKey().equals(admreObj.getPrtype())) {
                        behavior = admreObj.getPrname();
                    } else {
                        behavior = Tp_admpr.getByKey(admreObj.getPrtype()).getDescription();
                    }
                }
            }
        }
        return behavior;
    }

    /**
     * 返回行政行为文号
     *
     * @return
     */
    public String transPrcode() {
        // 行政行为文号
        String prcode = "";
        // 拼接行政行为文号
        if (ObjectUtil.isNotNull(admreObj) && !StringUtil.isEmpty(admreObj.getPrcode())) {
            prcode = "编号：(" + admreObj.getPrcode() + "）";
        }
        return prcode;
    }

    /**
     * 返回提交时间
     *
     * @return
     * @throws ParseException
     */
    public String transSubtime() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat formatT = new SimpleDateFormat("yyyy年M月d日");
        try {
            return ObjectUtil.isNotNull(faMain) ? formatT.format(format.parse(faMain.getSubtime())) : formatT.format(new Date());
        } catch (Exception e) {
            e.printStackTrace();
            return formatT.format(new Date());
        }
    }

    /**
     * 获取当前时间
     * @return
     */
    public String transModelToday(Boolean isInit) {
        if (isInit) {
            SimpleDateFormat formatT = new SimpleDateFormat("yyyy-MM-dd");
            return formatT.format(new Date());
        }
        SimpleDateFormat formatT = new SimpleDateFormat("yyyy年M月d日");
        return formatT.format(new Date());
    }


    /**
     * 公共参数查询方法
     * @param applyid
     */
    public void initDocParam (String applyid) {
        Apl_admreviewService apl_admreviewService = SpringUtil.getBean(Apl_admreviewService.class);
        Apl_mainService apl_mainService =  SpringUtil.getBean(Apl_mainService.class);
        Apl_applicantService apl_applicantService =  SpringUtil.getBean(Apl_applicantService.class);

        Apl_admreview admreview = apl_admreviewService.getById(applyid);
        Apl_main apl_main = apl_mainService.getById(applyid);
        List<Apl_applicant> applicantList = apl_applicantService.getApplicantByApplyid(applyid);
        this.setAdmreObj(admreview);
        this.setApl_admre_audit_code(apl_main.getAcceptcode());
        this.setFaMain(apl_main);
        this.setApplicantList(applicantList);
    }


    /**
     * 生成对应的文件模板
     *
     * @return
     */
    public abstract AbstractDoc generateTemplate(String applyid);



}
